const Router = require('koa-router')
const uuid = require('uuid')
const { check, catchError, generateOk } = require('../../lib/check')
const { setAccount,  editAccount, getAllAccounts, searchAccountById } = require('../../model/accounts')
const {  setPassword } = require('../../model/password')
let account = new Router()

account.post("/get", async (ctx) => {
  try {
    const data = ctx.request.body
    const accounts = getAllAccounts()
    let newAccounts = accounts
    if (data.age === 'descend') {
      newAccounts = newAccounts.sort((a, b) => Number(b.age) - Number(a.age))
    } else {
      newAccounts = newAccounts.sort((a, b) => Number(a.age) - Number(b.age))
    }
    if (data.createAt === 'descend') {
      newAccounts = newAccounts.sort((a, b) => Number(b.createAt) - Number(a.createAt))
    } else {
      newAccounts = newAccounts.sort((a, b) => Number(a.createAt) - Number(b.createAt))
    }
    if (data.vip !== undefined) {
      newAccounts = newAccounts.filter(item => item.vip === data.vip)
    }
    if (data.sex !== undefined) {
      newAccounts = newAccounts.filter(item => item.sex === data.sex)
    }
    if (data.role !== undefined) {
      newAccounts = newAccounts.filter(item => item.role === data.role)
    }
    if (data.status !== undefined) {
      newAccounts = newAccounts.filter((item) => item.status === data.status)
    }
    if (data.city !== undefined) {
      newAccounts = newAccounts.filter((item) => item.city.includes(data.city))
    }
    if (data.nickname !== undefined) {
      newAccounts = newAccounts.filter((item) => item.nickname.includes(data.nickname))
    }
    const start = data.offset === undefined ? 0 : data.offset
    const size = data.limit === undefined ? 7 : data.limit
    const resData = newAccounts.slice(start, start + size)
    ctx.body = generateOk({ data: resData, total: newAccounts.length, length: resData.length })
  } catch (e) {
    catchError(e, ctx)
  }
})

account.post('/whiteList', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.id, 'Params_Lost')
    const findAccount = searchAccountById(data.id)
    check(findAccount, 'Not_User')
    editAccount({ ...findAccount, inWhiteList: !findAccount.inWhiteList })
    ctx.body = generateOk({ ...findAccount, inWhiteList: !findAccount.inWhiteList })
  } catch (e) {
    catchError(e, ctx)
  }
})

account.post('/froozen', async (ctx) => {
  try {
    const data = ctx.request.body
    check(data.id, 'Params_Lost')
    const findAccount = searchAccountById(data.id)
    check(findAccount, 'Not_User')
    editAccount({ ...findAccount, froozen: !findAccount.froozen, status: findAccount.status === 0 ? 1 : 0 })
    ctx.body = generateOk({ ...findAccount, froozen: !findAccount.froozen, status: 0 })
  } catch (e) {
    catchError(e, ctx)
  }
})

account.post('/add', async (ctx) => {
  try {
    const data = ctx.request.body
    const allAccount = getAllAccounts()
    check(data.nickname, 'Params_Lost')
    check(data.account, 'Params_Lost')
    check(data.password, 'Params_Lost')
    const account = {
      focus: [],
      nickname: data.nickname || "用户" + data.account,
      account: data.account,
      id: allAccount.length.toString().padStart(6, '0'),
      slogan: '',
      city: '',
      sex: '',
      avatar: '',
      age: "0",
      role: 'normal',
      inWhiteList: true,
      froozen: false,
      score: 1000,
      vip: true,
      status: 1,
      createAt: new Date().getTime().toString()
    }
    setAccount(account)
    setPassword({ account: data.account, password: data.password, status: 1 })
    ctx.body = generateOk(account)
  } catch (e) {
    catchError(e, ctx)
  }
})

module.exports = account